<template>
  <div class="page-list-live">
    <PageList>
      <template #header>区域管理</template>
      <template #filter>
        <ListFilter :form="topFilter" :key-list="keyList" @search="handleSearch" @reset="handleReset"></ListFilter>
      </template>
      <template #tips>
        <el-button icon="el-icon-delete" type="danger" @click="batchDeletion">批量删除</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="handleAdd">新增</el-button>
      </template>
      <template #table>
        <ListTable ref="RefListTable" :loading="tableLoading" :table-data="tableData" :table-col="tableCol" @selectChange="handleSelectionChange">
          <template slot="col1" slot-scope="scope">
            <el-button icon="el-icon-edit" :loading="scope.row.loadingEdit" size="mini" type="text" @click="handleRowEdit(scope.row)">编辑</el-button>
            <el-button icon="el-icon-delete" style="color: red;" :loading="scope.row.loadingDel" size="mini" type="text" @click="handleRowDel(scope.row)">删除</el-button>
          </template>
        </ListTable>
      </template>
    </PageList>
    <Region ref="RefAccountsEdit" @success="afterEditSuccess"></Region>
  </div>
</template>
<script>
  import listMixin from '@/mixins/list'
  import Region from './components/Region'
  import ListFilter from '@/components/ListFilter'
  import { area } from '@/api/api'
  export default {
    components: { Region, ListFilter },
    mixins: [listMixin],
    data() {
      return {
        deleteList: {
          id: ''
        },
        topFilter: {
          keyword: { label: '关键字', placeholder: '请输入区域编码或区域名称' }
        },
        keyList: ['code', 'name'],
        tableData: [],
        tableCol: [
          { type: 'selection' },
          { prop: 'code', label: '区域编码' },
          { prop: 'name', label: '区域名称' },
          { prop: 'limitation', label: '限制人数' },
          { prop: 'remark', label: '备注' },
          { label: '操作', custom: 'col1' }
        ]
      }
    },
    methods: {
      batchDeletion() {
        if (!this.deleteList.id) {
          this.$message.warning('请选择要删除的数据！')
          return
        }
        this.$_delConfirm('选中区域', () => {
          this.delRowReq(this.deleteList)
        })
      },
      // 选中事件
      handleSelectionChange(event) {
        if (event.length > 0) {
          let ary = []
          event.forEach(item => {
            ary.push(item.id)
          })
          if (ary.length > 1) {
            this.deleteList.id = ary.join(',')
          } else {
            this.deleteList.id = ary[0]
          }
        } else {
          this.deleteList.id = ''
        }
      },
      // 请求列表数据
      async getList(query) {
        this.tableLoading = true
        try {
          const obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false }
          const { data: { items: resData, count, pageIndex } } = await area('post', obj, false)
          this.tableData = []
          this.tableData = resData.map(item => {
            item.loadingEdit = false
            item.loadingDel = false
            return item
          })
          this.$nextTick(() => {
            this.$refs['RefListTable'].update({ page: pageIndex, count })
            this.tableLoading = false
          })
        } catch (err) {
          this.tableLoading = false
          console.error('getList err: ', err)
        }
      },
      async afterEditSuccess() {
        await this.$router.push({ query: {} })
        this.getList()
      },
      handleAdd() {
        this.$refs['RefAccountsEdit'].open()
      },
      handleRowEdit(row) {
        this.$refs['RefAccountsEdit'].open(row)
      },
      passwordHandleEdit(row) {
        this.$refs['RefPassword'].open(row)
      },
      async delRowReq(row) {
        try {
          const obj = { id: row.id, value: null }
          await area('delete', obj, true)
          row.loadingDel = false
          await this.$router.push({ query: {} })
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } catch (err) {
          row.loadingDel = false
          console.error('handleRowDel err: ', err)
        }
      },
      handleRowDel(row) {
        row.loadingDel = true
        this.$_delConfirm(
          '该区域',
          () => {
            this.delRowReq(row)
          },
          () => {
            row.loadingDel = false
          }
        )
      }
    }
  }
</script>

<style lang="scss" scoped></style>
